<%@ tag import="com.dili.dd.gpi.util.Pagination" %>
<%@ tag import="com.dili.dd.gpi.util.StringUtils" %>
<%@tag pageEncoding="UTF-8" %>
<%@ attribute name="paginationSize" type="java.lang.Integer" required="true" %>
<%@ attribute name="searchParams" type="java.lang.String" required="false" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%
    Pagination pagination = (Pagination) request.getAttribute("pagination");
    int current = pagination.getCurrentPage();
    int begin = Math.max(1, current - paginationSize / 2);
    int end = Math.min(begin + (paginationSize - 1), pagination.getPageCount());

    request.setAttribute("current", current);
    request.setAttribute("begin", begin);
    request.setAttribute("end", end);
    String queryString = pagination.getQueryString();
    if (!StringUtils.isNullOrEmpty(queryString)) {
        request.setAttribute("searchParams", queryString);
    }
%>

<div class="dataTables_paginate paging_bootstrap pagination">
    <ul class="pagination">
        <% if (pagination.isCanGoPrevious()) {%>
        <li><a href="?page=1<c:if test='${!empty searchParams}'>&${searchParams}</c:if>" >&lt;&lt;</a></li>
        <li><a href="?page=${current-1}<c:if test='${!empty searchParams}'>&${searchParams}</c:if>">&lt;</a></li>
        <%} else {%>
        <li class="disabled"><a href="#">&lt;&lt;</a></li>
        <li class="disabled"><a href="#">&lt;</a></li>
        <%} %>

        <c:forEach var="i" begin="${begin}" end="${end}">
            <c:choose>
                <c:when test="${i == current}">
                    <li class="active"><a href="?page=${i}<c:if test='${!empty searchParams}'>&${searchParams}</c:if>">${i}</a></li>
                </c:when>
                <c:otherwise>
                    <li><a href="?page=${i}<c:if test='${!empty searchParams}'>&${searchParams}</c:if>">${i}</a></li>
                </c:otherwise>
            </c:choose>
        </c:forEach>

        <% if (pagination.isCanGoNext()) {%>
        <li><a href="?page=${current+1}<c:if test='${!empty searchParams}'>&${searchParams}</c:if>">&gt;</a></li>
        <li><a href="?page=${pagination.pageCount}<c:if test='${!empty searchParams}'>&${searchParams}</c:if>">&gt;&gt;</a></li>
        <%} else {%>
        <li class="disabled"><a href="#">&gt;</a></li>
        <li class="disabled"><a href="#">&gt;&gt;</a></li>
        <%} %>

    </ul>
</div>

